/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package arg_program;

import arg.program.gui.MainJFrame;
import arg.program.gui.DetailsJFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.sql.*;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.ListSelectionModel;
import javax.swing.text.Position.Bias;

/**
 *
 * @author Jasmin
 */
public class AnimeDBQuery extends MainJFrame {

    private Connection conn;
    private Scanner inputFile;
    final String usrInput = searchTB.getText();
    final String DB_URL = "jdbc:derby:ARGDB";
            
    Random random = new Random();
    
    ArrayList<String> genreList = new ArrayList<>();
    ArrayList<String> synopsisList = new ArrayList<>();
    ArrayList<String> japTitleList = new ArrayList<>();
    ArrayList<String> companyList = new ArrayList<>();
    ArrayList<String> numEpList = new ArrayList<>();
    
    DefaultListModel usrInputList = new DefaultListModel();
    DefaultListModel listModel = new DefaultListModel();
    DefaultComboBoxModel genreModel = new DefaultComboBoxModel();
    DefaultComboBoxModel chooseModel = 
            new DefaultComboBoxModel(new String[]{"Anime", "Manga"});

    /**
     * Constructor
     */
    public AnimeDBQuery() {

        RandomAnime();    
        GetTableInfo();
        FillGenreComboBox();
        FillAnimeOrMangaComboBox();
        
        
        jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jList1.addMouseListener(new MouseClickListener());
        genreCB.addActionListener(new ComboBoxListener());
        jButton1.addActionListener(new GoButtontListener());
    }

    /**
     * The FillJList method adds the Anime_Table's EngTitle column into the
     * dbList.
     */
    public void GetTableInfo() {
        
        try {
            conn = DriverManager.getConnection(DB_URL);

            Statement stmt = conn.createStatement();

            String sqlStatement = "SELECT * FROM AnimeTable";

            ResultSet result = stmt.executeQuery(sqlStatement);

            while (result.next()) {
                
                String engTitle = result.getString("ENG_TITLE");
                String japTitle = result.getString("JAP_TITLE");
                String synopsis = result.getString("SYNOPSIS");
                String genre = result.getString("GENRE");
                String numEp = result.getString("NUM_EP");
                String company = result.getString("COMP");

                listModel.addElement(engTitle);
                japTitleList.add(japTitle);
                synopsisList.add(synopsis);
                genreList.add(genre);
                companyList.add(company);
                numEpList.add(numEp);
            }
        } catch (Exception ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
        
        jList1.setModel(listModel);
    }

    /**
     * The FillJGenreComboBox method adds the Anime_Table's Genre column into
     * the genreComboBox.
     */
    public void FillGenreComboBox() {
        try {
            File genreFile = new File("genres.txt");

            inputFile = new Scanner(genreFile);

            while (inputFile.hasNext()) {
                String line = inputFile.nextLine();

                genreModel.addElement(line);
            }

            genreCB.setModel(genreModel);
            inputFile.close();
            
        } catch (Exception ex) {
            System.out.println("Could not find the file.");
        }
    }

    public void FillAnimeOrMangaComboBox() {
        
        mediaCB.setModel(chooseModel);
    }

    /**
     * The RandomAnime method will assign a random anime title's Photo to 5
     * different jLabels.
     */
    public void RandomAnime() {
        try {
            conn = DriverManager.getConnection(DB_URL);

            Statement stmt = conn.createStatement();

            String sqlStatement = "SELECT PHOTO FROM AnimeTable ORDER BY RANDOM()";

            ResultSet result = stmt.executeQuery(sqlStatement);           

            while(result.next())
            {               
                String image = result.getString("PHOTO");
                
                ImageIcon icon = new ImageIcon(image, "");
                
                jLabel2.setIcon(icon);
                jLabel3.setIcon(icon);
            }

        } catch (Exception ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
    }
    

    /**
     * Public inner class that handles the event when the user mouse clicks
     * different list items.
     */   
    public class MouseClickListener implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            
            if (e.getClickCount() == 2) {
                
                String selection = (String) jList1.getSelectedValue();
            
                new DetailsJFrame().setVisible(true);
            }
        }
        
        public void mousePressed(MouseEvent e) {}

        public void mouseReleased(MouseEvent e) {}

        public void mouseEntered(MouseEvent e) {}

        public void mouseExited(MouseEvent e) {}
    }
    
    /**
     * Public inner class that handles the event when the user clicks
     * the Go button.
     */
    public class GoButtontListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            
            usrInputList.addElement(usrInput);
            
            
            for(int i = 0; i < listModel.size(); i++)
            {           
                String item = (String)listModel.elementAt(i);
                String sub = item.substring(0, usrInput.length());
                
                if(sub.equalsIgnoreCase(usrInput))
                {
                    jList1.setSelectedIndex(i);
                }
                
                //int someInt = jList1.getNextMatch(usrInput, i, Bias.Forward);
                //jList1.setSelectedIndex(someInt);
            }
        }
    }
    
    /**
     * Public inner class that handles the event when the user selects
     * an item in the Genre Combo Box.
     */
    public class ComboBoxListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            
            String selectedGenre;
            selectedGenre = (String) genreCB.getSelectedItem();
            
            jLabel4.setText(selectedGenre);
        }
    }
}
